package guidePage;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.DateUtils;
import utils.KeyBoard;
import utils.PickerUtils;
import view.MyAddressPicker;
import view.MyOptionPicker;

/**
 * Created by Administrator on 2017/4/15.
 * 注册完第一次填写信息
 */

@ContentView(R.layout.complete_personal_message)
public class CompletePersonalMessage extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.complete_personal_message_year1)
    private TextView complete_personal_message_year1;
    @ViewInject(R.id.complete_personal_message_year2)
    private TextView complete_personal_message_year2;
    @ViewInject(R.id.complete_personal_message_year3)
    private TextView complete_personal_message_year3;
    @ViewInject(R.id.complete_personal_message_area)
    private TextView complete_personal_message_area;
    @ViewInject(R.id.complete_personal_message_type)
    private TextView complete_personal_message_type;
    @ViewInject(R.id.complete_personal_message_count)
    private TextView complete_personal_message_count;
    @ViewInject(R.id.complete_personal_message_area_layout)
    private RelativeLayout complete_personal_message_area_layout;
    @ViewInject(R.id.complete_personal_message_type_layout)
    private RelativeLayout complete_personal_message_type_layout;
    @ViewInject(R.id.complete_personal_message_count_layout)
    private RelativeLayout complete_personal_message_count_layout;
    @ViewInject(R.id.complete_personal_message_save)
    private TextView complete_personal_message_save;
    @ViewInject(R.id.complete_personal_message_back)
    private ImageView complete_personal_message_back;

    private String year;
    private String school_prov;
    private String school_city;
    private String school_dist;
    private String type;
    private String count;
    private int color;
    private int white;
    private final int COMPLETEINFO = 0 ;
    private MyAddressPicker addressPicker;//地区选择器

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        String err_code = jsonObject.getString("err_code");
                        if("0".equals(err_code)){
                            //修改成功
                            CompletePersonalMessage.this.setResult(COMPLETEINFO);
                            CompletePersonalMessage.this.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initYear();
    }

    private void initView() {
        complete_personal_message_area_layout.setOnClickListener(this);
        complete_personal_message_type_layout.setOnClickListener(this);
        complete_personal_message_count_layout.setOnClickListener(this);
        complete_personal_message_year1.setOnClickListener(this);
        complete_personal_message_year2.setOnClickListener(this);
        complete_personal_message_year3 .setOnClickListener(this);
        complete_personal_message_save.setOnClickListener(this);
        complete_personal_message_back.setOnClickListener(this);
    }

    private void initYear() {
        color = getResources().getColor(R.color.common_color);
        white = getResources().getColor(R.color.white);

        String year = DateUtils.getYear();
        complete_personal_message_year1.setText(year);//今年
        this.year = year;
        int i = Integer.parseInt(year);
        i += 1;
        String s = String.valueOf(i);
        complete_personal_message_year2.setText(s);//明年
        i += 1;
        String s1 = String.valueOf(i);
        complete_personal_message_year3.setText(s1);//后年

        complete_personal_message_year1.setTextColor(white);
        complete_personal_message_year1.setBackgroundColor(color);
        complete_personal_message_year2.setTextColor(color);
        complete_personal_message_year2.setBackgroundResource(R.drawable.blue_border);
        complete_personal_message_year3.setTextColor(color);
        complete_personal_message_year3.setBackgroundResource(R.drawable.blue_border);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_personal_message_back:
                this.finish();
                break;
            case R.id.complete_personal_message_save:
                completeInfo();
                break;
            case R.id.complete_personal_message_year1:
                year = complete_personal_message_year1.getText().toString();
                complete_personal_message_year1.setTextColor(white);
                complete_personal_message_year1.setBackgroundColor(color);
                complete_personal_message_year2.setTextColor(color);
                complete_personal_message_year2.setBackgroundResource(R.drawable.blue_border);
                complete_personal_message_year3.setTextColor(color);
                complete_personal_message_year3.setBackgroundResource(R.drawable.blue_border);

                break;
            case R.id.complete_personal_message_year2:
                year = complete_personal_message_year2.getText().toString();
                complete_personal_message_year2.setTextColor(white);
                complete_personal_message_year2.setBackgroundColor(color);
                complete_personal_message_year1.setTextColor(color);
                complete_personal_message_year1.setBackgroundResource(R.drawable.blue_border);
                complete_personal_message_year3.setTextColor(color);
                complete_personal_message_year3.setBackgroundResource(R.drawable.blue_border);

                break;
            case R.id.complete_personal_message_year3:
                year = complete_personal_message_year3.getText().toString();
                complete_personal_message_year3.setTextColor(white);
                complete_personal_message_year3.setBackgroundColor(color);
                complete_personal_message_year2.setTextColor(color);
                complete_personal_message_year2.setBackgroundResource(R.drawable.blue_border);
                complete_personal_message_year1.setTextColor(color);
                complete_personal_message_year1.setBackgroundResource(R.drawable.blue_border);
                break;

            case R.id.complete_personal_message_area_layout:
                //选地区
                addressPicker = PickerUtils.showAddressSelector(this);
                if (addressPicker != null) {
                    addressPicker.setOnAddressPickListener(new MyAddressPicker.OnAddressPickListener() {
                        @Override
                        public void onAddressPicked(String province, String city, String county) {
                            school_prov = province;
                            school_city = city;
                            school_dist = county;
                            complete_personal_message_area.setText(school_prov+"-"+school_city+"-"+school_dist);
                        }
                    });
                }
                break;
            case R.id.complete_personal_message_count_layout:
                //选分数
                showScoreDialog();
                break;
            case R.id.complete_personal_message_type_layout:
                //选科目
                MyOptionPicker optionPicker = PickerUtils.showSubjectSelector(this);
                optionPicker.setOnOptionPickListener(new MyOptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(String option) {
                        type = option;
                        complete_personal_message_type.setText(type);
                    }
                });

                break;

            default:
                break;
        }
    }

    private void showScoreDialog() {
        KeyBoard.showKeyBoard();
        final Dialog dialog = new Dialog(this, R.style.centerDialog);
        dialog.setContentView(R.layout.fourfragment_score_layout);
        ImageView fourfragment_score_layout_cancel = (ImageView) dialog.getWindow().findViewById(R.id.fourfragment_score_layout_cancel);
        fourfragment_score_layout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消按钮
                dialog.dismiss();
            }
        });
        final EditText fourfragment_score_layout_edt = (EditText) dialog.getWindow().findViewById(R.id.fourfragment_score_layout_edt);
        //配置editView
        fourfragment_score_layout_edt.setSelection(fourfragment_score_layout_edt.getText().length());

        ImageView fourfragment_score_layout_confirm = (ImageView) dialog.getWindow().findViewById(R.id.fourfragment_score_layout_confirm);
        fourfragment_score_layout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //完成填写分数
                count = fourfragment_score_layout_edt.getText().toString();
                if(TextUtils.isEmpty(count)){
                    count = "0";
                }
                complete_personal_message_count.setText(count);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void completeInfo() {
        HashMap<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(year)) {
            hashMap.put("exam_year", year);
        }
        if (!TextUtils.isEmpty(school_prov)) {
            hashMap.put("school_prov", school_prov);
        }

        if (!TextUtils.isEmpty(school_city)) {
            hashMap.put("school_city", school_city);
        }

        if (!TextUtils.isEmpty(school_dist)) {
            hashMap.put("school_dist", school_dist);
        }
        if (!TextUtils.isEmpty(type)) {
            hashMap.put("type", type);
        }
        if (!TextUtils.isEmpty(count)) {
            hashMap.put("count", count);
        }

        new InterNetController(this, Constant.COMPLETEINFO, myHandler, hashMap, COMPLETEINFO);
    }
}
